<?php

namespace throttle\src\exception;

class ExceptionHandler extends \Exception
{
    public function __construct($message, $code = 400, $previous = null)
    {
        $originalMessage = $previous ? $previous->getMessage() : '';
        $originalFile = $previous ? $previous->getFile() : '';
        $originalLine = $previous ? $previous->getLine() : '';
        // 构建完整的错误消息
        $date = date("Y-m-d H:i:s");
        $fullMessage = $date .$message;
        if (!empty($originalMessage)) {
            $fullMessage .= ",原始错误信息: $originalMessage 在 $originalFile:$originalLine";
        }
        $fullMessage .= PHP_EOL;
        $logPath =  __DIR__ . '/../logs';
        if(!is_dir($logPath)){
            if (!mkdir($logPath) && !is_dir($logPath)) {
                throw new BusinessException('创建日志路径失败');
            }
        }
        $logFilePath = $logPath.'/' . date('Ymd') . '.log';
        file_put_contents($logFilePath, $fullMessage, FILE_APPEND);
        parent::__construct($message, $code, $previous);
    }

}